import { FACE_DIR } from "../../Script/framework/utils/Define";
import Fighter from "./Fighter";

 

/**
 * 子弹
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Bullet extends cc.Component {

    //发射速度
    speed:number = 1000;
    //是否开火
    isOpenFire:boolean = false; 
    //发射的子弹战机
    fighter:Fighter = null;
    //子弹方向
    bulletDir:FACE_DIR = FACE_DIR.LEFT;
 
    
    setFigher(fighter:Fighter){
        this.fighter = fighter;
    }

    /**
     * 设置子弹朝向
     * @param dir 
     */
    setFaceDir(dir:FACE_DIR){
        this.node.scaleX = -dir;
        this.bulletDir = dir;
    }

    setParent(parnet:cc.Node){
        this.node.parent = parnet;
    }

    setPostion(x:number,y:number){
        this.node.x = x;
        this.node.y = y;
    }
 
    openFire(){
        this.isOpenFire = true; 
    }

    stopFire(){
        this.isOpenFire = false;
    }

    /**
     * 检查是否出界
     */
    checkRange(){
        let BulletLayer = this.node.parent;
        let leftMax =  - BulletLayer.width/2  
        let rightMax =   BulletLayer.width/2  
        let topMax =   - BulletLayer.height/2  
        let bottomMax =  BulletLayer.height/2 
        let mx = this.node.x;
        let my = this.node.y;

        if(leftMax > mx || rightMax < mx){
            GPool.put(this.node);
        } 
    }
 
    update (dt) {
        if(this.isOpenFire == false) return;   
        this.node.x -= dt*this.speed * this.bulletDir;  
        this.checkRange();
    }
}
